package com.tfjybj.itoo.exam.provider.controller;

import org.apache.dubbo.config.annotation.Reference;;
import com.dmsdbj.itoo.tool.business.ItooResult;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.TeacherCourseEntity;
import com.tfjybj.itoo.exam.model.StaffModel;
import com.tfjybj.itoo.exam.model.TeacherCourseModel;
import com.tfjybj.itoo.exam.provider.service.TeacherCourseService;
import com.tfjybj.itoo.teach.FacadeApi.TeacherCourseApi;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * TeacherCourseController
 * teacherCourse表
 *
 * @author 杨江涛
 * @version 2.0.0
 * @since 2.0.0 2018-11-16 20:35:34
 */
@Api(tags = {"teacherCourse表接口"})
@RequestMapping(value = "/teacherCourse")
@RestController
public class TeacherCourseController {

    @Resource
    private TeacherCourseService teacherCourseService;

    //贾建清-2019年5月31日15:25:56
    @Reference(check = false)
    private TeacherCourseApi teacherCourseApi;

    //region 模板生成：基本增删改

    /**
     * 添加
     *
     * @param model TeacherCourseModel
     * @return 添加的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-16 20:35:34
     */
    @ApiOperation(value = "添加")
    @PostMapping(value = {"/create"})
    public ItooResult create(@RequestBody TeacherCourseModel model) {
        if (StringUtils.isEmpty(model.getCourseId())) {
            return ItooResult.build(ItooResult.FAIL, "courseId为空");
        }
        if (StringUtils.isEmpty(model.getTeacherId())) {
            return ItooResult.build(ItooResult.FAIL, "teacherId为空");
        }
        com.tfjybj.itoo.teach.entity.TeacherCourseEntity teacherCourseEntity = new com.tfjybj.itoo.teach.entity.TeacherCourseEntity();
        BeanUtils.copyProperties(model, teacherCourseEntity);
        teacherCourseApi.save(teacherCourseEntity);
        return ItooResult.build(ItooResult.SUCCESS, "添加成功");
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return ItooResult 是否删除成功
     * @author 杨江涛
     * @since 2.0.0 2018-11-16 20:35:34
     */
    @ApiOperation(value = "根据id删除（单个）")
    @DeleteMapping(value = {"/delete/{id}"})
    public ItooResult delete(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        teacherCourseApi.removeById(id);
        return ItooResult.build(ItooResult.SUCCESS, "删除成功");
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return ItooResult 批量删除是否成功结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-16 20:35:34
     */
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping(value = {"/deleteByIds"})
    @ApiImplicitParam(name = "ids", value = "ids", dataType = "List<String>", required = true)
    public ItooResult deleteByIds(@RequestBody List<String> ids) {
        teacherCourseApi.removeByIds(ids);
        return ItooResult.build(ItooResult.SUCCESS, "批量删除成功");
    }

    /**
     * 修改
     *
     * @param model TeacherCourseModel
     * @return 修改后的结果
     * @author 杨江涛
     * @since 2.0.0 2018-11-16 20:35:34
     */
    @ApiOperation(value = "根据id修改teacherCourse")
    @PutMapping(value = {"/modify"})
    public ItooResult modify(@RequestBody TeacherCourseModel model) {
        if (StringUtils.isEmpty(model.getCourseId())) {
            return ItooResult.build(ItooResult.FAIL, "courseId为空");
        }
        if (StringUtils.isEmpty(model.getTeacherId())) {
            return ItooResult.build(ItooResult.FAIL, "teacherId为空");
        }
        com.tfjybj.itoo.teach.entity.TeacherCourseEntity teacherCourseEntity = new com.tfjybj.itoo.teach.entity.TeacherCourseEntity();
        BeanUtils.copyProperties(model, teacherCourseEntity);
        teacherCourseApi.updateById(teacherCourseEntity);
        return ItooResult.build(ItooResult.SUCCESS, "修改成功");
    }

    /**
     * 根据id查找TeacherCourse
     *
     * @param id 主键id
     * @author 杨江涛
     * @since 2.0.0 2018-11-16 20:35:34
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping(value = {"/findById/{id}"})
    public ItooResult findById(@ApiParam(value = "主键id", required = true) @PathVariable String id) {
        com.tfjybj.itoo.teach.entity.TeacherCourseEntity teacherCourseEntity = teacherCourseApi.getById(id);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teacherCourseEntity);
    }

    /**
     * 根据id查找TeacherCourse
     *
     * @param teacherId teacher_id
     * @author 杨江涛
     * @since 2.0.0 2018-11-16 20:35:34
     */
    @ApiOperation(value = "根据教师id查询课程")
    @GetMapping(value = "/findByTeacherId/{teacherId}")
    public ItooResult findByTeacherId(@ApiParam(value = "教师id", required = true) @PathVariable String teacherId) {
        List<TeacherCourseEntity> teacherCourseEntity = teacherCourseService.findByTeacherId(teacherId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teacherCourseEntity);
    }

    /**
     * 根据学生id查询该学生,该学年课程
     * @param studentId 学生Id
     * @author  杨海云
     * @since 2.0.0 2019-2-24
     */
    @GetMapping(value = "/findByStudentId/{studentId}")
    public ItooResult findByStudentId(@ApiParam(value = "学生Id", required = true) @PathVariable String studentId){
        if (studentId.isEmpty()){
           return ItooResult.build(ItooResult.FAIL,"请输入学号!",studentId);
        }else{
            List<TeacherCourseEntity> teacherCourseEntityList = teacherCourseService.findByStudentId(studentId);
            return ItooResult.build(ItooResult.SUCCESS,"查询成功!",teacherCourseEntityList);
        }

    }

    /**
     * 分页查询所有TeacherCourse
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @author 杨江涛
     * @since 2.0.0 2018-11-16 20:35:34
     */
    @ApiOperation(value = "分页查询所有TeacherCourse")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo", value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                   @ApiParam(name = "pageSize", value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<com.tfjybj.itoo.teach.entity.TeacherCourseEntity> teacherCourses = teacherCourseApi.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teacherCourses);
    }

    /**
     * 根据课程_id模糊查询teacherCourse
     *
     * @param courseId 课程_id
     * @param pageNo   页码
     * @param pageSize 页数
     * @return 模糊查询的teacherCourse
     * @author 杨江涛
     * @since 2.0.0 2018-11-16 20:35:34
     */
    @ApiOperation(value = "根据课程_id模糊查询teacherCourse", notes = "分页根据课程_id模糊查询teacherCourse")
    @GetMapping(value = "queryTeacherCourseByLikeCourseId/{pageNo}/{pageSize}")
    public ItooResult queryTeacherCourseByLikeCourseId(@RequestParam(required = false, defaultValue = "") String courseId,
                                                       @ApiParam(value = "页码", required = true, example = "1") @PathVariable Integer pageNo,
                                                       @ApiParam(value = "记录数", required = true, example = "10") @PathVariable Integer pageSize) {
        PageInfo<TeacherCourseEntity> teacherCourseList = teacherCourseService.queryByLikeCourseId(courseId, pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teacherCourseList);
    }
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 根据teacherId查找该教师所授课程下的所有教师信息
     *
     * @param teacherId 教师ID
     * @author 杨江涛
     * @since 2.0.0 2018-11-27 20:52:40
     */
    @ApiOperation(value = "根据teacherId查找该教师所授课程下的所有教师信息")
    @GetMapping(value = "/findTeacherByTeacherId/{teacherId}")
    public ItooResult findTeacherByTeacherId(@ApiParam(value = "教师id", required = true) @PathVariable String teacherId) {
        List<TeacherCourseEntity> teacherCourseEntity = teacherCourseService.findTeacherByTeacherId(teacherId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teacherCourseEntity);
    }

    /**
     * 根据courseId查找该课程下所有教师信息
     *
     * @param courseId 课程ID
     * @author 杨海云
     * @since 2.0.0 2018-11-27 20:52:40
     */
    @ApiOperation(value = "根据courseId查找该课程下所有教师信息")
    @GetMapping(value = "/findTeacherByCourseId/{courseId}")
    public ItooResult findTeacherByCourseId(@ApiParam(value = "课程id", required = true) @PathVariable String courseId) {
        List<StaffModel> staffModels = teacherCourseService.findTeacherByCourseId(courseId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", staffModels);
    }

    /**
     * 根据查询参数模糊查找该教师及课程信息
     *
     * @param searchInfo 查询参数
     * @return teacherCourseModelList
     * @author 杨江涛
     * @since 2.0.0 2018-12-7 10:39:04
     */
    @ApiOperation(value = "根据查询参数模糊查找该教师及课程信息（返回list）")
    @GetMapping(value = "/findTeacherCourse/{searchInfo}")
    public ItooResult findTeacherCourse(@ApiParam(value = "查询参数", required = true) @PathVariable String searchInfo) {
        List<TeacherCourseModel> teacherCourseModelList = teacherCourseService.findTeacherCourse(searchInfo);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teacherCourseModelList);

    }

    /**
     * 根据登录人id查询课程
     *
     * @param teacherId 教师id
     * @return 登陆人所有的课程
     * @author maying
     * @since 2018-12-12 08:55:58
     */
    @ApiOperation(value = "根据登录人id查询课程")
    @GetMapping(value = "/loginCourse/{teacherId}")
    public ItooResult loginCourse(@PathVariable String teacherId) {
        List<TeacherCourseModel> teacherCourseModelList = teacherCourseService.loginCourse(teacherId);
        if (teacherCourseModelList.size() > 0) {
            return ItooResult.build(ItooResult.SUCCESS, "查询成功!", teacherCourseModelList);
        }
        return ItooResult.build(ItooResult.FAIL, "查询失败");
    }

    /**
     * 根据教师id查询该教师教过的所有课程
     * @param teacherId 教师_id
     * @author 杜雨
     * @since 2019年6月20日
     */
    @ApiOperation(value = "根据教师id查询该教师教过的所有课程")
    @GetMapping(value = "/findAllByTeacherId/{teacherId}")
    public ItooResult findAllByTeacherId(@ApiParam(value = "教师id", required = true) @PathVariable String teacherId) {
        List<TeacherCourseEntity> teacherCourseEntity = teacherCourseService.findAllByTeacherId(teacherId);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", teacherCourseEntity);
    }

}    
